import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/user/Login.vue'
import routerMap from './router.config.js'

Vue.use(Router)
var router = new Router({
  mode: 'history',
  base: '/admin/',
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '*',
      redirect: '/login'
    },
    routerMap
  ]
})
router.beforeEach((to, from, next) => {
  var token = localStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
  }
})
export default router
